package com.zzptc.my.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.zzptc.my.myapplication.entity.Cars;
import com.zzptc.my.myapplication.entity.Cheliangpinpai;
import com.zzptc.my.myapplication.service.CarsService;
import com.zzptc.my.myapplication.service.CheliangpinpaiService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // getCars();
        getCheliangpinpai();
    }

    private void getCheliangpinpai() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CheliangpinpaiService cheliangpinpaiService = retrofit.create(CheliangpinpaiService.class);
        Call<Cheliangpinpai> call = cheliangpinpaiService.getAllPinpai("fac9eff7d9e1570e027ddcb4543243a7", "A");

        call.enqueue(new Callback<Cheliangpinpai>() {
            @Override
            public void onResponse(Call<Cheliangpinpai> call, Response<Cheliangpinpai> response) {
                Cheliangpinpai body = response.body();
                Log.d(TAG, "onResponse: "+body.getResult().get(0).getBrand_name());
            }

            @Override
            public void onFailure(Call<Cheliangpinpai> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });
    }

    private void getCars() {//创建retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
//创建接口对象
        CarsService carsService=retrofit.create(CarsService.class);
        //调用接口中的抽象方法
        Call<Cars>all=carsService.getAll();
//调用Call类型中的异步方法
        all.enqueue(new Callback<Cars>() {
            @Override
            public void onResponse(Call<Cars> call, Response<Cars> response) {
              //调用正文
                Cars body=response.body();
                Log.d(TAG, "onResponse: "+new Gson().toJson(body));
                Log.d(TAG, "onResponse: "+body.getData().get(0).getCarplate());

            }

            @Override
            public void onFailure(Call<Cars> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });
    }
}
